package com.shop.shopmall.controller;

import com.shop.shopmall.pojo.GoodsRule;
import com.shop.shopmall.service.GoodsRuleService;
import com.shop.shopmall.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品规格控制器
 *负责商品规格的增删改查操作
 * @author Jamie
 * @date 2025/3/30 16:08
 */
@RestController
@RequestMapping("/rule")
public class GoodsRuleController {
    
    @Autowired
    private GoodsRuleService goodsRuleService;

    /**
     * 分页查询商品规格列表
     * @param goodsRule 商品规格查询条件封装对象，用于传递分页及筛选信息
     * @return 返回包含分页查询结果的统一响应对象
     */
    @PostMapping("/pageList")
    public Result<?> pageList(@RequestBody GoodsRule goodsRule) {
        return Result.success(goodsRuleService.getGoodsRuleList(goodsRule));
    }

    /**
     * 新增商品规格
     * @param goodsRule 待新增的商品规格对象
     * @return 返回包含操作结果的统一响应对象
     */
    @PostMapping
    public Result<?> addRule(@RequestBody GoodsRule goodsRule) {
        return Result.success(goodsRuleService.save(goodsRule));
    }

    /**
     * 更新商品规格
     * @param goodsRule 待更新的商品规格对象，包含需要更新的属性及标识
     * @return 返回包含操作结果的统一响应对象
     */
    @PutMapping
    public Result<?> updateRule(@RequestBody GoodsRule goodsRule) {
        return Result.success(goodsRuleService.updateById(goodsRule));
    }

    /**
     * 根据ID删除商品规格
     *
     * @param id 商品规格的唯一标识，用于指定待删除的规格
     * @return 返回包含操作结果的统一响应对象
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable Integer id) {
        return Result.success(goodsRuleService.removeById(id));
    }
}
